<template>
  <div class="swiper-container" ref="mySwiper">
    <div class="swiper-wrapper">
      <div class="swiper-slide" v-for="banner in youLikeList" :key="banner.id">
        <div class="p-img">
          <img class="margin-left" :src="banner.image" />
        </div>
        <div class="attr">
          <em class="margin-left1">{{ banner.title }}</em>
        </div>
        <div class="price">
          <em class="margin-left2">¥</em>
          <i>{{ banner.price }}</i>
        </div>
      </div>
    </div>
    <!-- 如果需要分页器 -->
    <div class="swiper-pagination"></div>

    <!-- 如果需要导航按钮 -->
    <div class="swiper-button-prev"></div>
    <div class="swiper-button-next"></div>
  </div>
</template>

<script>
import Swiper from "swiper";
import "swiper/css/swiper.min.css";
export default {
  name: "XCarousel",
  props: ["youLikeList"],
  watch: {
    youLikeList: {
      handler(newVal) {
        if (!newVal.length) return; //防止生成swiper实例多次 把立即执行这次啊去除掉
        // watch + $nextTick watch能保证数据变了 $nextTick能保证这次页面更新了
        //等到这次页面更新完成 才会执行里面的回调函数
        this.$nextTick(() => {
          new Swiper(this.$refs.mySwiper, {
            loop: true, //无缝轮播
            speed: 1000, //切换时间  到下一张所需要的时间
            spaceBetween: 30, //每屏间隔大小
            slidesPerView: 4, // 每屏多少张
            autoplay: {
              //自动轮播
              delay: 1000, //延迟时间   当前屏显示多久 去下一屏
              disableOnInteraction: false, //控制鼠标操作之后 还自动轮播不
            },
            pagination: {
              el: ".swiper-pagination", //获取小圆点的容器
              clickable: true, //是否可以点击小圆点
            },
            navigation: {
              //获取左右按钮容器
              nextEl: ".swiper-button-next",
              prevEl: ".swiper-button-prev",
            },
          });
        });
      },
      immediate: true, //为了和floor一致
    },
  },
};
</script>

<style lang="less" scoped>
.swiper-button-next:after,
.swiper-button-prev:after {
  font-family: swiper-icons;
  font-size: var(--swiper-navigation-size);
  text-transform: none !important;
  letter-spacing: 0;
  text-transform: none;
  font-variant: initial;
  line-height: 1;
  font-size: 20px;
  color: #d5cebd;
}
.swiper-slide {
  margin-top: 20px;
  margin-bottom: 20px;
  margin-left: 5px;
}
.p-img {
  margin-right: 100px;
  text-align: center;
  img {
    width: 167px;
    height: 123px;
  }
}
.attr {
  margin-top: 20px;
  padding: 0 15px;
}
.price {
  margin-top: 10px;
  padding: 0 15px;
  font-size: 16px;
  font-weight: bold;
  color: #c81623;
  margin-bottom: 20px;
  margin-left: 50px;
}
em {
  font-weight: bold;
  font-size: 12px;
}
</style>
